package com.gensdai.leliang.base;


import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;

import com.gensdai.leliang.R;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

/**
 * Created by Administrator on 2017/5/23.
 */

public abstract class BaseActivity extends RxAppCompatActivity{

    public Toolbar mToolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getLayoutResource() > 0) {
            setContentView(getLayoutResource());

            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            if (mToolbar != null) {
                //把Toolbar设置成默认的actionbar
                setSupportActionBar(mToolbar);
                //Toolbar不为空的时候才执行这些方法
                if (isShowBackButton()) {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    getSupportActionBar().setHomeButtonEnabled(true);

                }


                if (isCustomTitle()) {

                } else {

                    if (getTitleStr() != null && !TextUtils.isEmpty(getTitleStr())) {
                        getSupportActionBar().setTitle(getTitleStr());
                    } else {
                        getSupportActionBar().setTitle(getResources().getString(R.string.undefined_title));
                    }
                }

            }
        }
    }

    @Override
    public void setSupportActionBar(Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
    }

    protected abstract String getTitleStr();

    protected abstract boolean isShowBackButton();

    protected abstract boolean isCustomTitle();

    protected abstract int getLayoutResource();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            onBackPressed();
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
